package com.fanfanwang.ffw.app.net;


import android.app.Activity;
import android.content.DialogInterface;
import android.os.AsyncTask;

public abstract class IssAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
	private IssLoadingDialog ld;
	protected String exception;

	public IssAsyncTask(Activity activity) {
		this(activity, true);
	}

	public IssAsyncTask(Activity activity, final DialogInterface.OnCancelListener l) {
		this(activity, l, true);
	}

	public IssAsyncTask(Activity activity, final boolean cancelable) {
		this(activity, null, cancelable);
	}

	public IssAsyncTask(Activity activity, final boolean cancelable, boolean isShow) {//最后一个参数是是否显示dialog  倒数第二个参数是控制它是否支持返回消失
		this(activity, null, cancelable, isShow);
	}

	public IssAsyncTask(Activity activity, final DialogInterface.OnCancelListener l, final boolean cancelable) {
		super();
		if(activity == null){
			return;
		}
		ld = new IssLoadingDialog(activity);
		ld.setCancelable(cancelable);
		ld.setOnCancelListener(new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				if (cancelable) {
					IssAsyncTask.this.cancel(true);
				}
				if (l != null) {
					l.onCancel(dialog);
				}
			}
		});
	}

	public IssAsyncTask(Activity activity, final DialogInterface.OnCancelListener l, final boolean cancelable, boolean isShow) {
		super();
		if (isShow) {
			ld = new IssLoadingDialog(activity);
			ld.setCancelable(cancelable);
			ld.setOnCancelListener(new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					if (cancelable) {
						IssAsyncTask.this.cancel(true);
					}
					if (l != null) {
						l.onCancel(dialog);
					}
				}
			});
		}
	}

	public IssAsyncTask(final boolean cancelable) {
		super();
		if (cancelable) {
			IssAsyncTask.this.cancel(true);
		}
	}

	@Override
	protected Result doInBackground(Params... params) {
		return null;
	}

	@Override
	protected void onPostExecute(Result result) {
		super.onPostExecute(result);
		try {
			if (ld != null)
				ld.dismiss();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		try {
			if (ld != null && !ld.isShowing())
				ld.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
